/**
 * 
 */
package de.nak.roommgmt.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.web.struts.LookupDispatchActionSupport;

import de.nak.roommgmt.model.Room;
import de.nak.roommgmt.service.RoomService;

/**
 * Action class that handles user actions in the room list page.
 * @author Stephan Anft
 */
public class EditRoomsAction extends LookupDispatchActionSupport {

	/**
	 * Starts the "Add room" operation.
	 * @param mapping The action mapping.
	 * @param form The action form.
	 * @param request The request.
	 * @param response The response.
	 * @return the forward.
	 * @throws Exception if an exception has occured.
	 */
	public ActionForward addRoom(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		RoomForm roomForm = (RoomForm)form;
		// Create new empty room in action form
		Room room = new Room();
		roomForm.setRoom(room);
		return mapping.findForward("add");
	}

	/**
	 * Starts the "Edit room" operation.
	 * @param mapping The action mapping.
	 * @param form The action form.
	 * @param request The request.
	 * @param response The response.
	 * @return the forward.
	 * @throws Exception if an exception has occured.
	 */
	public ActionForward editRoom(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		RoomForm roomForm = (RoomForm)form;
		// Load the room from database
		RoomService roomService = (RoomService)getWebApplicationContext().getBean("roomService");
		Room room = roomService.showRoom(roomForm.getSelectedKey());
		roomForm.setRoom(room);
		return mapping.findForward("edit");
	}
	
	/**
	 * Performs the "Delete room" operation.
	 * @param mapping The action mapping.
	 * @param form The action form.
	 * @param request The request.
	 * @param response The response.
	 * @return the forward.
	 * @throws Exception if an exception has occured.
	 */
	public ActionForward deleteRoom(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		RoomForm roomForm = (RoomForm)form;
		// Get the selected id
		Long roomId = roomForm.getSelectedKey();
		// Delete room
		RoomService roomService = (RoomService)getWebApplicationContext().getBean("roomService");
		roomService.deleteRoom(roomId);
		// Reload list
		roomForm.setRooms(roomService.listRooms());
		return mapping.findForward("list");
	}	

	/** {@inheritDoc} */
	@Override
	protected Map<String, String> getKeyMethodMap() {
		Map<String, String> map = new HashMap<String, String>();
		map.put("button.add", "addRoom");
		map.put("button.edit", "editRoom");
		map.put("button.delete", "deleteRoom");
		return map;
	}

}
